﻿using Sl.Upload.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sl.Upload.Models;
using Sl.Upload.Help;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace Sl.Upload
{
    public class FileUpload : IUpload
    {
        public CompleteInfo Upload(FileInfoModel fim)
        {
            CompleteInfo ci = new CompleteInfo();
            OtherHelp.CreateDirectory(fim);
            if (fim.Chunks > 0)
            {
                string path = fim.SavePath + "\\" + fim.Guid;
                OtherHelp.CreateDirectory(path);
                FileHelp.SaveFile(fim.FileByte, path + "\\" + fim.Chunk);
            }
            else {
                if (FileHelp.SaveFile(fim.FileByte, fim.SavePath + "\\" + fim.UploadName))
                {
                    ci.UploadStatus = UploadStatus.Success;
                    ci.Name = fim.OriginalName;
                    ci.Url = fim.UrlPath + "/" + fim.UploadName;
                    ci.Size = fim.ContentLength;
                    ci.Suffix = fim.FileSuffix;
                }
                else
                {
                    ci.UploadStatus = UploadStatus.Error;
                }
            }
            return ci;
        }
    }
}
